package com.github.cakin.shiro.chapter18.web.controller;

import com.github.cakin.shiro.chapter18.entity.Organization;
import com.github.cakin.shiro.chapter18.service.OrganizationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @className: OrganizationController
 * @description: 组织管理控制器
 * @date: 2020/5/26
 * @author: cakin
 */
@Controller
@RequestMapping("/organization")
public class OrganizationController {
    /**
     * 注入组织服务
     */
    @Autowired
    private OrganizationService organizationService;

    /**
     * 功能描述：展示组织管理首页
     *
     * @author cakin
     * @date 2020/5/26
     * @param model 给前端展示的数据
     * @return String 前端页面
     */
    // 指定需要的权限信息
    @RequiresPermissions("organization:view")
    @RequestMapping(method = RequestMethod.GET)
    public String index(Model model) {
        return "organization/index";
    }

    /**
     * 功能描述：展示树形组织
     *
     * @author cakin
     * @date 2020/5/26
     * @param model 给前端展示的数据
     * @return String 前端页面
     */
    @RequiresPermissions("organization:view")
    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    public String showTree(Model model) {
        model.addAttribute("organizationList", organizationService.findAll());
        return "organization/tree";
    }

    /**
     * 功能描述：点击增加子节点触发此API
     *
     * @author cakin
     * @date 2020/5/26
     * @param parentId 父节点Id
     * @param model 返回给前端数据
     * @return String 展示前端页面为：organization/appendChild
     */
    @RequiresPermissions("organization:create")
    @RequestMapping(value = "/{parentId}/appendChild", method = RequestMethod.GET)
    public String showAppendChildForm(@PathVariable("parentId") Long parentId, Model model) {
        Organization parent = organizationService.findOne(parentId);
        model.addAttribute("parent", parent);
        Organization child = new Organization();
        child.setParentId(parentId);
        child.setParentIds(parent.makeSelfAsParentIds());
        model.addAttribute("child", child);
        model.addAttribute("op", "新增");
        return "organization/appendChild";
    }
    /**
     * 功能描述：点击增加子节点触发此API
     *
     * @author cakin
     * @date 2020/5/26
     * @param organization 前端传过来的数据
     * @return String 重定向前端页面为：/organization/success
     */
    @RequiresPermissions("organization:create")
    @RequestMapping(value = "/{parentId}/appendChild", method = RequestMethod.POST)
    public String create(Organization organization) {
        organizationService.createOrganization(organization);
        return "redirect:/organization/success";
    }
    /**
     * 功能描述：点击树结构中某个组织节点
     *
     * @author cakin
     * @date 2020/5/26
     * @param id 组织id
     * @param model 返回给前端该id的组织数据
     * @return String 展示前端页面为：organization/maintain
     */
    @RequiresPermissions("organization:update")
    @RequestMapping(value = "/{id}/maintain", method = RequestMethod.GET)
    public String showMaintainForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("organization", organizationService.findOne(id));
        return "organization/maintain";
    }
    /**
     * 功能描述：点击修改按钮触发
     *
     * @author cakin
     * @date 2020/5/26
     * @param organization 组织
     * @param redirectAttributes 返回给重定向页面属性
     * @return String 重定向前端页面为：/organization/success
     */
    @RequiresPermissions("organization:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    public String update(Organization organization, RedirectAttributes redirectAttributes) {
        organizationService.updateOrganization(organization);
        redirectAttributes.addFlashAttribute("msg", "修改成功");
        return "redirect:/organization/success";
    }

    /**
     * 功能描述：根据id删除某个组织
     *
     * @author cakin
     * @date 2020/5/26
     * @param id 待删除的组织id
     * @param redirectAttributes 重定向属性
     * @return String 重定向地址
     * @description: redirectAttributes 重定向地址携带属性
     */
    @RequiresPermissions("organization:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        organizationService.deleteOrganization(id);
        redirectAttributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/organization/success";
    }

    /**
     * 功能描述：点击移动节点触发
     *
     * @author cakin
     * @date 2020/5/26
     * @param sourceId 被移动组织节点的Id
     * @param model 返回给前端的数据
     * @return String 前端展示页面
     * @description: redirectAttributes 重定向地址携带属性
     */
    @RequiresPermissions("organization:update")
    @RequestMapping(value = "/{sourceId}/move", method = RequestMethod.GET)
    public String showMoveForm(@PathVariable("sourceId") Long sourceId, Model model) {
        Organization source = organizationService.findOne(sourceId);
        // 被移动的组织
        model.addAttribute("source", source);
        // 排查自己以及孩子外的节点
        model.addAttribute("targetList", organizationService.findAllWithExclude(source));
        return "organization/move";
    }

    /**
     * 功能描述：点击移动按钮触发此API
     *
     * @author cakin
     * @date 2020/5/26
     * @param sourceId 被移动的组织Id
     * @param targetId 被移动的组织成为targetId的孩子
     * @return targetId 重定向到：/organization/success
     * @description:
     */
    @RequiresPermissions("organization:update")
    @RequestMapping(value = "/{sourceId}/move", method = RequestMethod.POST)
    public String move(
            @PathVariable("sourceId") Long sourceId,
            @RequestParam("targetId") Long targetId) {
        Organization source = organizationService.findOne(sourceId);
        Organization target = organizationService.findOne(targetId);
        organizationService.move(source, target);
        return "redirect:/organization/success";
    }

    /**
     * 功能描述：跳转到操作成功页
     *
     * @author cakin
     * @date 2020/5/26
     * @return String
     * @description: 操作成功的页面
     */
    @RequiresPermissions("organization:view")
    @RequestMapping(value = "/success", method = RequestMethod.GET)
    public String success() {
        return "organization/success";
    }
}
